package mn.idax.exchange.otc.service.impl;

import mn.idax.exchange.otc.entity.OTCUser;
import mn.idax.exchange.otc.entity.OTCUserExample;
import mn.idax.exchange.otc.entity.User;
import mn.idax.exchange.otc.entity.UserExample;
import mn.idax.exchange.otc.enums.OTCUserEnum;
import mn.idax.exchange.otc.enums.response.UserResponseEnum;
import mn.idax.exchange.otc.exception.IdaxException;
import mn.idax.exchange.otc.mapper.UserMapper;
import mn.idax.exchange.otc.service.OTCUserService;
import mn.idax.exchange.otc.service.UserService;
import mn.idax.exchange.otc.util.LogUtils;
import mn.idax.exchange.otc.util.NickNameUtils;
import mn.idax.exchange.otc.vo.UserCertificateInfo;
import mn.idax.exchange.otc.vo.UserCertificateName;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * Create by jack_huang on 2018/8/10
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OTCUserService otcUserService;
    //
//    @Override
//    public User getUserByNickName(String nickName) {
//
//        UserExample example = new UserExample();
//        example.createCriteria().andNicknameEqualTo(nickName);
//        //通过nickName 查询是否有这个用户
//        List<User> users = userMapper.selectByExample(example);
//        return users.isEmpty() ? null : users.get(0);
//    }

    @Override
    public User getUserById(Integer userId) {
        UserExample example = new UserExample();
        example.createCriteria().andIdEqualTo(userId);
        //通过nickName 查询是否有这个用户
        List<User> users = userMapper.selectByExample(example);
        return users.isEmpty() ? null : users.get(0);
    }

    @Override
    public boolean updateNickName(User user,String nickName) {
        UserExample example = new UserExample();
        example.createCriteria()
                .andIdEqualTo(user.getId())
                .andNicknameIsNull();
        User updateUser = new User();
        updateUser.setNickname(nickName);
        int changeRow = userMapper.updateByExampleSelective(updateUser,example);
        if (changeRow == 1){
            return true;
        }
        return false;
    }

    @Override
    public String verifyUsableUpdateNickName(int userId) {
        User user = this.getUserById(userId);
        OTCUser otcUser = otcUserService.getOTCUserByUserId(userId);
        if (!otcUser.getIsupdated()){
            return null;
        }else if (user != null){
            return StringUtils.isEmpty(user.getNickname()) ?
                    NickNameUtils.concatNickName(user.getNickname(),user.getEmail()) : user.getNickname();
        }
        return null;
    }

    //修改用户昵称
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED,rollbackFor = {RuntimeException.class,Exception.class})
    public boolean updateOTCNickName(Integer userId,String nickName) {

        OTCUser otcUser = otcUserService.getOTCUserByUserId(userId);
        if (otcUser == null){
            throw new IdaxException(UserResponseEnum.USER_ISNOTEXIST.getRespCode());
        }
        //已经改过 不能再修改
        if (otcUser.getIsupdated()){
            throw new IdaxException(UserResponseEnum.USER_NICK_NAME_UNABLE_UPDATE.getRespCode());
        }
        User user = new User();
        user.setId(userId);
        user.setNickname(nickName);
        int updateNickName = userMapper.updateByPrimaryKeySelective(user);
        //更新otcuser表中的isupdated
        int updateIsUpdated = otcUserService.updateIsUpdated(userId,otcUser);

        return updateNickName == 1 && updateIsUpdated == 1;
    }

    @Override
    public int batchUpdateNickName() {
        return userMapper.batchUpdateNickName();
    }


    //获取user认证的真实name
    @Override
    public UserCertificateName getUserCertificateName(Integer buyer, Integer seller) {
        List<UserCertificateInfo> userCertificateInfolist = userMapper.getUserCertificateName(buyer,seller);
        String buyerName = null;
        String sellerName = null;
        if (userCertificateInfolist.get(0).getIsCertificate() && userCertificateInfolist.get(1).getIsCertificate()){
            //均实名
            return new UserCertificateName(userCertificateInfolist.get(0).getSurName() + userCertificateInfolist.get(0).getName(),
                    userCertificateInfolist.get(1).getSurName() + userCertificateInfolist.get(1).getName());
        } else if (userCertificateInfolist.get(0).getIsCertificate() && !userCertificateInfolist.get(1).getIsCertificate()){
            //买家实名
            return new UserCertificateName(userCertificateInfolist.get(0).getSurName()+userCertificateInfolist.get(0).getName(),
                    sellerName);
        } else if (!userCertificateInfolist.get(0).getIsCertificate() && userCertificateInfolist.get(1).getIsCertificate()){
            //卖家实名
            return new UserCertificateName(buyerName,
                    userCertificateInfolist.get(1).getSurName()+userCertificateInfolist.get(1).getName());
        } else {
            //都没实名
            return new UserCertificateName(buyerName,sellerName);
        }
    }



    @Override
    public void unforzenUserStatus(Integer userid) {

        UserExample example = new UserExample();
        example.createCriteria()
                .andIdEqualTo(userid)
        ;

        User user = new User();
        user.setIsthreecancelotcorder(false);

        int update = userMapper.updateByExampleSelective(user, example);

//        LogUtils.info(this.getClass(),"unforzenUserStatus total " + update + " ,userid: " + userid);

    }


    @Override
    public void forzenUserStatus(Integer userid) {

        UserExample example = new UserExample();
        example.createCriteria()
                .andIdEqualTo(userid)
        ;

        User user = new User();
        user.setIsthreecancelotcorder(true);

        int update = userMapper.updateByExampleSelective(user, example);

        LogUtils.info(this.getClass(),"forzenUserStatus total " + update + " ,userid: " + userid);

    }

    @Override
    public List<Integer> getMoreThanThreeCancelUser() {
        return userMapper.getMoreThanThreeCancelUser();
    }
}
